import { createSlice, PayloadAction } from '@reduxjs/toolkit';

interface Component {
  id: number;
  name: string;
  description: string;
}

interface ComponentState {
  components: Component[];
  loading: boolean;
  error: string | null;
}

const initialState: ComponentState = {
  components: [],
  loading: false,
  error: null,
};

const componentSlice = createSlice({
  name: 'component',
  initialState,
  reducers: {
    setComponents: (state, action: PayloadAction<Component[]>) => {
      state.components = action.payload;
    },
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.loading = action.payload;
    },
    setError: (state, action: PayloadAction<string | null>) => {
      state.error = action.payload;
    },
  },
});

export const { setComponents, setLoading, setError } = componentSlice.actions;
export default componentSlice.reducer;